<template>
  <el-dialog
    :title="dialogTitle"
    :visible.sync="dialogVisible"
    width="30%"
    @closed="afterCloseDialog"
  >
    <el-form
      ref="ruleFormRef"
      :rules="rules"
      :model="dialogForm"
      label-width="120px"
    >
      <el-form-item label="选择图谱：" prop="graphId">
        <el-select
          v-model="dialogForm.graphId"
          filterable
          placeholder="请选择图谱"
          style="width: 100%"
        >
          <el-option
            v-for="item in graphResList"
            :key="item.projectId"
            :label="item.projectName"
            :value="item.projectId"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="confirmDialog">确定</el-button>
    </template>
  </el-dialog>
</template>
<script>
export default {
  name: "SelectGraph",
  data() {
    return {
      dialogTitle: "选择规则应用图谱",
      dialogVisible: false,
      dialogForm: {
        graphId: null,
      },
      rules: {
        graphId: [
          { required: true, message: "请选择规则应用图谱！", trigger: "blur" },
        ],
      },
      graphResList: [], // 图谱数据
    };
  },
  methods: {
    // 打开弹窗
    openDialog(row) {
      // 请求图谱数据
      this.getGraphList();
      this.dialogVisible = true;
    },
    // 获取图谱列表
    getGraphList() {
      this.$axios
        .post(this.$api.knowledgeMap.getMapList, {
          graphName: "",
          graphStatus: "0",
          pageNum: "",
          pageSize: "",
        })
        .then((res) => {
          const data = res.data.data;
          if (Array.isArray(data.list)) {
            this.graphResList = data.list;
          }
        });
    },
    afterCloseDialog() {
      this.dialogForm = {
        graphId: null,
      };
    },
    confirmDialog() {
      this.$refs.ruleFormRef.validate((valid) => {
        if (valid) {
          this.$emit("confirm", this.dialogForm.graphId);
          this.dialogVisible = false;
        }
      });
    },
  },
};
</script>
